﻿using AppMachine.Controls.ValidationRules;
using AppMachine.Domain.LogicCom.AppComs;
using AppMachine.EntityFrameworkCore.Repositories.LogicCom;
using AppMachine.EntityFrameworkCore.Repositories.PerpDev;
using PerpheralDevice;
using PerpheralDevice.Units;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Media;

namespace AppMachine.Controls.Primitive {
    /// <summary>
    /// 输入开关控件
    /// </summary>
    public class PrimDI : Prim {

        protected ComSwitchRO mfComSwitchRO => this.mfAppComPrim as ComSwitchRO;
        static PrimDI() {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(PrimDI), new FrameworkPropertyMetadata(typeof(PrimDI)));            
        }

        protected override void OnLoadCompleted() {
            this.mfComSwitchRO.Rem<ComSwitchRO>(UpdateTarget);
            this.mfComSwitchRO.Add<ComSwitchRO>(UpdateTarget);
        }
        private void UpdateTarget(ComSwitchRO comSwitchRO) {
            App.Current.Dispatcher.InvokeAsync(() => {
                this.IsOpen = comSwitchRO.IsOpen;
            });            
        }

        /// <summary>
        /// 1. 优先根据ComID加载组件；2. 如果加载失败，再根据 PerpDevID、PerpUnitID加载组件
        /// </summary>
        /// <returns></returns>
        protected override void Load() {
            base.Load();

            this.PerpDevID = this.mfComSwitchRO.PerpDevID;
            this.PerpUnitID = this.mfComSwitchRO.PerpUnitID;
            this.IsOpen = this.mfComSwitchRO.IsOpen;
            this.TagName = this.mfComSwitchRO.TagName;
            this.IsDisabled = this.mfComSwitchRO.IsDisabled;
            this.IsReversed = this.mfComSwitchRO.IsReversed;
            this.MockID = this.mfComSwitchRO.MockID;
            this.IsAlarmDI = this.mfComSwitchRO.IsAlarmDI;

            this.UpdateTarget();
            this.UpdateSource();
        }
        /// <summary>
        /// 保存应用组件配置
        /// </summary>
        /// <returns></returns>
        protected override bool Save() {
            if (!this.UpdateSource()) return false;

            this.mfComSwitchRO.IsReversedSet(this.IsReversed).IsDisabledSet(this.IsDisabled).MockIDSet(this.MockID).IsAlarmDISet(this.IsAlarmDI ?? false);

            return base.Save();
        }

        protected override IEnumerable<PerpDev> LoadPerpTypeItemsSource() {
            return PerpDevRepository.GetInstance().GetAll<PerpDev>().Where(it => it.Unit<SwitchRO>().Any());
        }
        protected override IEnumerable<PerpDevUnit> LoadUnitTypeItemsSource(PerpDev perpDev) {
            return this.mfAppComPrim.PerpDev.Unit<SwitchRO>();
        }

        /// <summary>
        /// 是否禁用
        /// </summary>
        public bool IsDisabled {
            get { return (bool)GetValue(IsDisabledProperty); }
            set { SetValue(IsDisabledProperty, value); }
        }
        public static readonly DependencyProperty IsDisabledProperty =
            DependencyProperty.Register("IsDisabled", typeof(bool), typeof(PrimDI), new PropertyMetadata(default(bool), (d, e) => {}));

        /// <summary>
        /// 是否反转应用
        /// </summary>
        public bool IsReversed {
            get { return (bool)GetValue(IsReversedProperty); }
            set { SetValue(IsReversedProperty, value); }
        }
        public static readonly DependencyProperty IsReversedProperty =
            DependencyProperty.Register("IsReversed", typeof(bool), typeof(PrimDI), new PropertyMetadata(default(bool), (d, e) => { }));

        /// <summary>
        /// 模拟输入(1：开, 2: 关，3：随机)
        /// </summary>
        public int MockID {
            get { return (int)GetValue(MockIDProperty); }
            set { SetValue(MockIDProperty, value); }
        }
        public static readonly DependencyProperty MockIDProperty =
            DependencyProperty.Register("MockID", typeof(int), typeof(PrimDI), new PropertyMetadata(default(int), (d, e) => { }));

        /// <summary>
        /// 输入开关状态(是否开启)
        /// </summary>
        [Description("输入开关状态(是否开启)")]
        public bool? IsOpen {
            get { return (bool?)GetValue(IsOpenProperty); }
            set { SetValue(IsOpenProperty, value); }
        }
        public static readonly DependencyProperty IsOpenProperty =
            DependencyProperty.Register("IsOpen", typeof(bool?), typeof(PrimDI), new PropertyMetadata(default(bool?), (d, e) => { }));                

        /// <summary>
        /// 是否为报警DI: 如果为true，则IsOpen为true时，UI为红色，否则IsOpen为true时，UI为绿色
        /// </summary>
        [Description("是否为报警DI: 如果为true，则IsOpen为true时，UI为红色，否则IsOpen为true时，UI为绿色")]
        public bool? IsAlarmDI {
            get { return (bool?)GetValue(IsAlarmDIProperty); }
            set { SetValue(IsAlarmDIProperty, value); }
        }
        public static readonly DependencyProperty IsAlarmDIProperty =
            DependencyProperty.Register("IsAlarmDI", typeof(bool?), typeof(PrimDI), new PropertyMetadata(default(bool?), (d, e) => { }));

    }
}
